/*
 * login.java
 *
 * Created on __DATE__, __TIME__
 */

package hotel.view;

import hotel.dao.UserDao;
import hotel.model.User;
import hotel.util.DbUtil;

import java.awt.Font;
import java.sql.Connection;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class login extends javax.swing.JFrame {
	
	DbUtil dbUtil = new DbUtil();
	UserDao userDao = new UserDao();

	public login() {
		addComponentListener(new ComponentAdapter() {
			public void componentHidden(ComponentEvent arg0) {
			}
		});
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setFont(new Font("微软雅黑", Font.PLAIN, 22));
		//改变系统默认字体
		Font font = new Font("Dialog", Font.PLAIN, 12);
		java.util.Enumeration keys = UIManager.getDefaults().keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof javax.swing.plaf.FontUIResource) {
				UIManager.put(key, font);
			}
		}
		initComponents();
		// 设置Frame居中显示
		this.setLocationRelativeTo(null);
	}

	
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel1.setFont(new Font("宋体", Font.PLAIN, 22));

		jLabel2 = new javax.swing.JLabel();
		jLabel2.setFont(new Font("宋体", Font.PLAIN, 22));

		userName = new javax.swing.JTextField();
		password = new javax.swing.JPasswordField();
		login = new javax.swing.JButton();
		login.setFont(new Font("宋体", Font.PLAIN, 22));

		reset = new javax.swing.JButton();
		reset.setFont(new Font("宋体", Font.PLAIN, 22));

		setTitle("登录");

		jLabel1.setText("账户");

		jLabel2.setText("密码");

		login.setText("登录");
		login.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				loginActionPerformed(evt);
			}
		});

		reset.setText("重置");

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				39,
																				39,
																				39)
																		.add(
																				layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING)
																						.add(
																								jLabel1)
																						.add(
																								jLabel2))
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.LEADING,
																								false)
																						.add(
																								password,
																								0,
																								0,
																								Short.MAX_VALUE)
																						.add(
																								userName,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								154,
																								Short.MAX_VALUE)))
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				81,
																				81,
																				81)
																		.add(
																				login)
																		.add(
																				18,
																				18,
																				18)
																		.add(
																				reset)))
										.addContainerGap(55, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.add(32, 32, 32)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel1)
														.add(
																userName,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.add(18, 18, 18)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel2)
														.add(
																password,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED,
												18, Short.MAX_VALUE)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(reset).add(login))
										.addContainerGap()));

		pack();
	}
	private void loginActionPerformed(java.awt.event.ActionEvent evt) {
		String userName = this.userName.getText();
		String password = new String(this.password.getPassword());
		if("".equals(userName) || userName == null){
			JOptionPane.showMessageDialog(null, "用户名不能为空");
			return;
		}
		if("".equals(password) || password == null){
			JOptionPane.showMessageDialog(null, "密码不能为空");
			return;
		}
		User user = new User();
		user.setUserName(userName);
		user.setPassword(password);
		Connection conn = null;
		try {
			conn = dbUtil.getCon();
			User us = userDao.login(conn, user);
			if(us == null){
				JOptionPane.showMessageDialog(null, "用户名或密码错误");
			}else{
				this.dispose();
				new main().setVisible(true);
			}
		} catch (Exception e) {
		
			e.printStackTrace();
		}finally{
			try {
				dbUtil.closeCon(conn);
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
	}

	
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new login().setVisible(true);
			}
		});
	}

	
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JButton login;
	private javax.swing.JPasswordField password;
	private javax.swing.JButton reset;
	private javax.swing.JTextField userName;
	

}